package com.agileai.pm.module.proc.handler;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.cxmodule.ProcPlanAdminManage;
import com.agileai.pm.domain.Consts;
import com.agileai.pm.domain.Project;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

public class ProcPlanAdminManageEditHandler
        extends StandardEditHandler {
    public ProcPlanAdminManageEditHandler() {
        super();
        this.listHandlerClass = ProcPlanAdminManageListHandler.class;
        this.serviceId = buildServiceId(ProcPlanAdminManage.class);
    }
    
    @PageAction
    public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		if (isReqRecordOperaType(operaType)){
			DataRow record = getService().getRecord(param);
			this.setAttributes(record);	
		}else if("insert".equals(operaType)){
			String ppaId = param.stringValue("ppaId");
			String planPid = param.stringValue("PPA_PID");
			if(StringUtil.isNotNullNotEmpty(planPid)){
				ppaId = planPid;
			}
			param = new DataParam();
			param.put("operaType","insert");
			param.put("ppaId",ppaId);
			param.put("PPA_PID",planPid);
			
			this.setAttribute("PPA_SORT", "10");
			this.setAttribute("PPA_WORK_DAY", "1");
			this.setAttribute("PPA_TYPE", "TASK");
		}
		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}

	public ViewRenderer doSaveAction(DataParam param){
		String operateType = param.get(OperaType.KEY);
		Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
		String projectId = project.getProjectId();
		param.put("PI_ID",projectId);
		
		if (OperaType.CREATE.equals(operateType)){
			getService().createRecord(param);
		}
		else if (OperaType.UPDATE.equals(operateType)){
			getService().updateRecord(param);	
		}
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}
	
	@PageAction
	public ViewRenderer saveAfterGoOn(DataParam param){
		String responseText = "fail";
		String operateType = param.get(OperaType.KEY);
		Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
		String projectId = project.getProjectId();
		param.put("PI_ID",projectId);
		
		if (OperaType.CREATE.equals(operateType)){
			getService().createRecord(param);
		}
		responseText = "success";
		return new AjaxRenderer(responseText);
	}
    
    protected void processPageAttributes(DataParam param) {
        setAttribute("PPA_TYPE",
                     FormSelectFactory.create("PPA_TYPE")
                                      .addSelectedValue(getOperaAttributeValue("PPA_TYPE",
                                                                               "GROUP")));
        setAttribute("PPA_STATE",
                     FormSelectFactory.create("PPA_STATE")
                                      .addSelectedValue(getOperaAttributeValue("PPA_STATE",
                                                                               "NOTSTART")));
        
        String operaType = param.get("operaType");
        if (OperaType.CREATE.equals(operaType)){
        	User user = (User)this.getUser();
        	setAttribute("PPA_CREATE", this.getAttributeValue("PPA_CREATE",user.getUserId()));
        	setAttribute("PPA_CREATE_NAME", this.getAttributeValue("PPA_CREATE_NAME",user.getUserName()));
        	setAttribute("PPA_CREATE_TIME", this.getAttributeValue("PPA_CREATE_TIME",DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL)));
        	setAttribute("PPA_WORK_DAY", this.getAttributeValue("PPA_WORK_DAY","0"));
        	
    		Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
    		String projectId = project.getProjectId();
			String ppaId = param.get("ppaId");
			if (StringUtil.isNullOrEmpty(ppaId)){
				setAttribute("PPA_PID", projectId);	
			}else{
				setAttribute("PPA_PID", ppaId);	
			}
        }
    }

    protected ProcPlanAdminManage getService() {
        return (ProcPlanAdminManage) this.lookupService(this.getServiceId());
    }
}
